import {
  FollowerModel
} from '../../../models/follower'
import { MineModel } from '../../../models/mine.js'
const ModelMine = new MineModel()
const followerModel = new FollowerModel()
const App =  getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    userTeamList: null,
    pageIndex: 1,
    pageSize: 20,
    hasNext: false,
    loading: false,
    empty:false,
    statusBarHeight: App.globalData.statusBarHeight,
    isTabbar:false,
    inviteInfo: {}, // 上级信息
    inviteVisible: false // 专属顾问信息
  },

  // 关闭显示专属顾问信息
  closeInvite() {
    this.setData({
      inviteVisible: false
    })
  },

  // 联系专属顾问
  invite(e) {
    const parentId = e.detail
    if(parentId) {
      const params = {
        id: parentId
      }
      ModelMine.getBandWxInfo(params).then(res => {
        // 开
        if (res.data.switch == 1) {
          if(res.data.inviteCode) {
            this.setData({
              inviteInfo: res.data,
              inviteVisible: true
            })
          } else {
            wx.showToast({
              title: '对方还没有添加信息',
              icon: 'none'
            })
            this.setData({
              inviteVisible: false
            })
          }
        }else{
          wx.showToast({
            title: '对方还没有添加信息',
            icon: 'none'
          })
          this.setData({
            inviteVisible: false
          })
        }
      }).catch()
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  initData: function () {
    const self = this;
     //获取粉丝列表
     followerModel.getFollowerList({
      pageIndex: this.data.pageIndex,
      pageSize: this.data.pageSize
    })
    .then(res => {
      if (res.data.userTeamList.length == 0) {
        self.data.empty = true;
      } else {
        self.data.empty = false
      }
      this.setData({
        empty:self.data.empty,
        userTeamList: res.data.userTeamList,
        hasNext: res.data.hasNext,
        pageIndex: res.data.pageNum
      })
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageIndex:1, 
      pageSize: 20
    })
    this.initData()
    wx.stopPullDownRefresh()
  },

  //加载更多
  loadMore(isPull) {
    if (this.data.loading) {
      return
    }
    if (this.data.hasNext) {
      const params = {
        pageIndex: this.data.pageIndex,
        pageSize: this.data.pageSize,
      }
      if (isPull) {
        console.log(isPull)
        this.setData({
          loading: true
        })
      }
      followerModel.getFollowerList(params).then(res => {
        const tempArray = this.data.userTeamList.concat(res.data.userTeamList)
        console.log(res)
        this.setData({
          userTeamList: tempArray,
          hasNext: res.data.hasNext,
          loading: false
        })
      })
    }
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.hasNext) {
      return
    }
    this.data.pageIndex = this.data.pageIndex + 1
    this.loadMore(true)
  },

})